define(['router','api','user','util'],function(Router,api,user,util){
	var location=window.location,
	    hash=location.hash,
	    router,
	    currentController = null,
	    routes = {	    	
		    '/': 'pages/index/index',
		    '/index/subscribe':'pages/index/subscribe',
			'/order':'pages/order/index',
			'/order/history':'pages/order/history',
			'/orderdetail/:opensn':'pages/order/detail',
			'/order/historydetail/:opensn':'pages/order/historydetail',
			'/order/create/:a/:b':'pages/order/create',
			'/article':'pages/article/index',
			'/noticeDetail/:id':'pages/article/noticeDetail',
			'/articleDetail/:id':'pages/article/articleDetail',
			'/login':'pages/user/login',
			'/agent/:uid':function(){
			    location.hash='#/';
			},
			'/logout':function(){
			   user.loginOut();
			},
			'/user':'pages/user/index',
			'/user/detail':'pages/user/detail',
			'/user/card':'pages/user/card',
			'/user/customer':'pages/user/customer',
			'/user/commission':'pages/user/commission',
			'/user/share':'pages/user/share',
			'/user/register/:userinfo':'pages/user/register',
			'/user/register':'pages/user/register',
			'/user/xieyi':'pages/user/xieyi',
			'/pass/getlogin':'pages/pass/getlogin',
			'/pass/getfund':'pages/pass/getfund',
			'/fund/flow':'pages/fund/flow',
			'/fund/bind/:id':'pages/fund/bind',
			'/fund/charge/:backurl':'pages/fund/charge',
			'/fund/cash':'pages/fund/manual',
			'/coupon/list':'pages/coupon/list',
			'/bank/list':'pages/bank/list',
			'/article/rule':'pages/article/rule',
			'/user/xieyi':'pages/user/xieyi',
			'/*':'pages/index/error',
			'/*/*':'pages/index/error'
	    },
	    //用于把字符串转化为一个函数，而这个也是路由的处理核心
	    routeHandler = function (config) {
	    	
	    	if(typeof(config)!=='string')return config;
	        return function () {
	            var url = config,
	                params = arguments;
	            require([url], function (controller) {
	                if(currentController && currentController !== controller){
	                    currentController.onRouteChange && currentController.onRouteChange();
	                }
	                currentController = controller;
	                controller.init.apply(controller, params);
	            });
	        }
	    },
	    boot=function(){
	        for (var key in routes) {
			    routes[key] = routeHandler(routes[key]);
			}
			router = Router(routes);
			router.init();
			api.router=router;
			if(''===hash)location.hash='#/';
	    };
	    
	api.init(boot);
})